/*
 * TMap Designer - A Thematic Map design software
 * Copyright (C) 2006 World Health Organization
 * http://www.who.int
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package who.tmap.dao.cd.impl;

import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

import org.jdom.Element;

import who.tmap.bo.BizObject;
import who.tmap.bo.cd.CDColorPattern;
import who.tmap.bo.cd.CDTypes;
import who.tmap.bo.cd.ColorPatternRange;
import who.tmap.bo.cd.ColorPatternClassification;
import who.tmap.dao.TMapDAOException;

public class CDColorPatternDAOImpl extends BaseCartographicDisplayDAOImpl {

	CDColorPatternDAOImpl() {
		super();
	}
	
	@SuppressWarnings("unchecked")
	public BizObject loadXml(Element elt) throws TMapDAOException {
		CDColorPattern cd =null;
	
		try {
			cd =(CDColorPattern)getCD( elt );
			Element mainElt =elt.getChild( "ColorPattern" );			
			
			cd.setJoinIdColumnName( getNullStringIfEmpty( mainElt.getAttributeValue( "joinIdColumnName" ) ) );
			cd.setDataColumnName( getNullStringIfEmpty( mainElt.getAttributeValue( "dataColumnName" ) ) );		
						
			// default
			cd.setClassification( ColorPatternClassification.EQUAL_STEP );
			
			String classification =mainElt.getAttributeValue( "classification" );
			for( int ct =0; ct < ColorPatternClassification.names.length; ct++ ) {
				if ( ColorPatternClassification.names[ ct ].equalsIgnoreCase( classification ) ) {
					cd.setClassification( ct );
					break;
				}
			}
			
			cd.setMissingDataRangeIdx( mainElt.getAttribute( "missingDataRange" ).getIntValue() );
			
			Map<Integer,ColorPatternRange> ranges =new TreeMap<Integer,ColorPatternRange>();
			cd.setRanges( ranges );		
			
	        Iterator it =mainElt.getChild( "Ranges" ).getChildren( "Range" ).iterator();
	        while( it.hasNext() ) {
	            Element rangeElt =(Element)it.next();
	            
	            ColorPatternRange cb =(ColorPatternRange)cdf.get( CDTypes.COLOR_PATTERN_RANGE );
	            
	            cb.setRangeIdx( rangeElt.getAttribute( "idx" ).getIntValue() );
	        	if ( cd.getClassification() != ColorPatternClassification.NON_NUMERIC ) {
	        		cb.setFrom( rangeElt.getAttribute( "from" ).getDoubleValue() );
	        		cb.setTo( rangeElt.getAttribute( "to" ).getDoubleValue() );
	        	} else {
	        		cb.setCaption( rangeElt.getAttributeValue( "caption" ) );
	        	}
	            cb.setColor( miscHelper.rgbToColor( rangeElt.getAttributeValue( "color") ) );
	            cb.setFillPattern( rangeElt.getAttribute( "fillPattern" ).getIntValue() );
	            
	            ranges.put( new Integer( cb.getRangeIdx() ), cb );
	        }
			
		} catch( Exception e ) {
			throw new TMapDAOException( e );
		}
		
		
		return cd;
	}

	@SuppressWarnings("unchecked")
	public Element saveXml(BizObject object) throws TMapDAOException {
		CDColorPattern cd =(CDColorPattern)object;
		
        Element rootElt =super.saveXml( object );
        Element mainElt =new Element( "ColorPattern" );
        rootElt.addContent( mainElt );        
        
        mainElt.setAttribute( "version", VERSION );
        
        mainElt.setAttribute( "joinIdColumnName", getNonNullString( cd.getJoinIdColumnName() ) );
        mainElt.setAttribute( "dataColumnName", getNonNullString( cd.getDataColumnName() ) );
        mainElt.setAttribute( "classification", ColorPatternClassification.names[ cd.getClassification() ] );
        mainElt.setAttribute( "missingDataRange", Integer.toString( cd.getMissingDataRangeIdx() ) );
        
        Element rangesElt =new Element( "Ranges" );
        mainElt.addContent( rangesElt );
        
        Iterator it =cd.getRanges().values().iterator();
        while( it.hasNext() ) {
        	ColorPatternRange cb =(ColorPatternRange)it.next();
        	
        	Element rangeElt =new Element( "Range" );
        	rangesElt.addContent( rangeElt );
        	
        	rangeElt.setAttribute( "idx", Integer.toString( cb.getRangeIdx() ) );
        	if ( cd.getClassification() != ColorPatternClassification.NON_NUMERIC ) {
        		rangeElt.setAttribute( "from", Double.toString( cb.getFrom() ) );
        		rangeElt.setAttribute( "to", Double.toString( cb.getTo() ) );
        	} else {
        		rangeElt.setAttribute( "caption", cb.getCaption() );
        	}
        	
        	rangeElt.setAttribute( "color", miscHelper.colorToHexa( cb.getColor() ) );
        	rangeElt.setAttribute( "fillPattern", Integer.toString( cb.getFillPattern() )  );        	        	
        }
        
		return rootElt;
	}

}
